import { reactive, ref, toRefs } from 'vue'
type Options = {
  defaultPageSize: number
}
export default function usePagination(callback: () => void, options: Options = { defaultPageSize: 9 }) {
  const pagination = reactive({
    showPagination: true,
    showTotal: true,
    pageSize: options.defaultPageSize,
    currentPage: 1,
    total: 0,
    onChange: (page: number) => {
      pagination.currentPage = page
      callback && callback()
    },
    onPageSizeChange: (pageSize: number) => {
      pagination.currentPage = 1
      pagination.pageSize = pageSize
      callback && callback()
    }
  })
  const changeCurrentPage = pagination.onChange
  const changePageSize = pagination.onPageSizeChange
  const { showPagination, showTotal, pageSize, currentPage, total } = toRefs(pagination)
  return {
    showPagination,
    showTotal,
    pageSize,
    currentPage,
    total,
    pagination,
    changeCurrentPage,
    changePageSize
  }
}
